package user.view;


import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import db.DataBaseManager;
import db.FlightsManage;

import javax.swing.JScrollPane;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;


import java.awt.Color;

public class FlightsFrame extends JFrame implements TableModelListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private ManagerTable managers_table;
	private DefaultTableModel tableModel;
	// private ResultSetMetaData rsmData;
	private String[] cnames = {"Flight ID","Date","Time","Departure","Destination","Seats","Price","Disount"};
	private JLabel update_label;
	private static FlightsFrame instAdminMain;
	private Vector<String> t;
	
	public static FlightsFrame getInstance()
	{
		if(instAdminMain==null)instAdminMain=new FlightsFrame();
		return instAdminMain;
	}
	/**
	 * Launch the application.
	 */
	public void flush() {
			
			tableModel.setDataVector(FlightsManage.getAllFlights(), t);
			managers_table.getColumn("Disount").setMinWidth(120);

	

	}
	public static void main(String[] args) {
		FlightsFrame.getInstance().setVisible(true);
	}
	

	/**
	 * Create the frame.
	 */
	private FlightsFrame() {
		setTitle("Managers edit");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 796, 493);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 79, 760, 370);
		contentPane.add(scrollPane);

		managers_table = new ManagerTable();
		scrollPane.setViewportView(managers_table);

		JButton btnFlush = new JButton("Flush");
		btnFlush.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				flush();
			}
		});
		btnFlush.setBounds(10, 22, 93, 23);
		contentPane.add(btnFlush);

		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addRow();
			}
		});
		btnAdd.setBounds(113, 22, 93, 23);
		contentPane.add(btnAdd);

		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteRow();
			}
		});
		btnDelete.setBounds(216, 22, 93, 23);
		contentPane.add(btnDelete);
		
		update_label = new JLabel("");
		update_label.setForeground(Color.GREEN);
		update_label.setBounds(330, 26, 298, 15);
		contentPane.add(update_label);
		
		JLabel lblDoubleClicksTo = new JLabel("Double clicks to Edit");
		lblDoubleClicksTo.setBounds(20, 55, 340, 15);
		contentPane.add(lblDoubleClicksTo);
		t=new Vector<String>();
		for (String s : cnames) {
			t.add(s);
		}
		tableModel=new DefaultTableModel();
		managers_table.setModel(tableModel);
		managers_table.setRowHeight(25);
		tableModel.addTableModelListener(this);
		managers_table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				if(2==e.getClickCount())
				{
					int row = managers_table.rowAtPoint(e.getPoint());
					int id=Integer.parseInt(managers_table.getValueAt(row, 0).toString());
					FlightEdit.getInstance().setEdit(id);
					FlightEdit.getInstance().setVisible(true);
				}
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		RowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(
				tableModel);
		managers_table.setRowSorter(sorter);
		
		flush();
		
	}

	@Override
	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
//		 if (e.getColumn() != -1)
//	        {
//	            Object o;
//	          
//	            
//	                
//	                o = managers_table.getValueAt(e.getFirstRow(), 2);
//	                if(o==null||o.toString().equals(""))
//	                {
//	                	JOptionPane.showMessageDialog(this, "password required!");
//	                	
//	                	flush();
//	                	
//	                	
//	                	return;
//	                }
//	                System.out.println(o.toString());
//	            
//	            
//	            
//	               
//	                int select = e.getFirstRow();
//	                if (select == -1)
//	                {
//	                    return;
//	                }
//	                select = managers_table.getRowSorter().convertRowIndexToModel(select);
//
//	                try
//	                {
//	                    rs.absolute(select + 1);
//	                    String r=managers_table.getValueAt(e.getFirstRow(), e.getColumn()).toString();
//	                    rs.updateObject(e.getColumn() + 1, r);
//	                
//	                    rs.updateRow();
//	                    update_label.setText("database updated:set data to "+r);
//	                //rs.close();
//	                } catch (SQLException ex)
//	                {
//	                   ex.printStackTrace();
//	                }
//	            
//	        }
	}
	private void deleteRow(){
		
		int select = managers_table.getSelectedRow();
	    if (select == -1)
	    {
	    	JOptionPane.showMessageDialog(this, "please click to select one row");
	        return;
	    }
	    select = managers_table.getRowSorter().convertRowIndexToModel(select);
	    int t=JOptionPane.showConfirmDialog(this, "Are you Sure to delete Flight id="+tableModel.getValueAt(select, 0)+"?");
	    if(JOptionPane.OK_OPTION!=t)return;
	    if(FlightsManage.deleteFlight(Integer.parseInt(tableModel.getValueAt(select, 0).toString())))
	    	flush()
	    	;
	    
	}
	private void addRow() {
		

		FlightEdit.getInstance().setInsertModel();
		FlightEdit.getInstance().setVisible(true);
		
	}
	class ManagerTable extends JTable
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -2094745550871520964L;

		public boolean isCellEditable(int   row,   int   column)   { 
		       
			return false;
				
	     }
	}
}
